package com.tmt.im.wallet.controler;

import com.tmt.im.common.pojo.WalletPoJo;
import com.tmt.im.wallet.controler.vo.*;
import com.tmt.im.wallet.helper.PrincipalHelper;
import com.tmt.im.wallet.service.WalletService;
import com.tmt.springboot.common.restful.IExecuteResult;
import com.tmt.springboot.common.restful.ResultWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.concurrent.CompletableFuture;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/9/5 19:56
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "钱包相关管理接口", description = "钱包相关管理接口")
@RestController
@RequestMapping("${api-url-prefix}/wallet")
public class WalletController {

    private final WalletService walletService;

    public WalletController(WalletService walletService) {
        this.walletService = walletService;
    }

    @Operation(summary = "修改用户钱包支付密码",
            description = "修改用户钱包支付密码",
            operationId = "updatePayPassword",
            parameters = {
                    @Parameter(name = "request", description = "更新支付密码请求对象", required = true)
            })
    @PostMapping(value = "/payPassword/update")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> updatePayPassword(@RequestBody @Validated UpdatePayPasswordRequest request, Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<Boolean> result = walletService.updatePayPassword(currentUserId, request, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "获取当前用户的钱包详情信息",
            description = "获取当前用户的钱包余额、绑定的支付宝、微信账号等信息",
            operationId = "getWalletDetail")
    @GetMapping(value = "/detail")
    public CompletableFuture<IExecuteResult<WalletPoJo>> getWalletDetail(Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<WalletPoJo> result = walletService.getWalletDetail(currentUserId, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "滑动查询钱包余额变更历史列表",
            description = "滑动查询钱包余额变更历史列表",
            operationId = "slideQueryWalletChangeHistory",
            parameters = {
                    @Parameter(name = "request", description = "查询钱包余额变更历史请求对象", required = true)
            })
    @PostMapping(value = "/changeHistory/list")
    public CompletableFuture<IExecuteResult<SlideQueryWalletChangeResponse>> slideQueryWalletChangeHistory(@RequestBody SlideQueryRequest request,
                                                                                                           Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<SlideQueryWalletChangeResponse> result = walletService.slideQueryWalletChangeHistory(currentUserId, request, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "分页查询钱包余额变更历史列表",
            description = "分页查询钱包余额变更历史列表",
            operationId = "pageQueryWalletChangeHistory",
            parameters = {
                    @Parameter(name = "request", description = "查询钱包余额变更历史请求对象", required = true)
            })
    @PostMapping(value = "/changeHistory/list/page")
    public CompletableFuture<IExecuteResult<PageQueryWalletChangeResponse>> pageQueryWalletChangeHistory(@RequestBody @Validated WalletChangeDetailQueryRequest request,
                                                                                                           Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<PageQueryWalletChangeResponse> result = walletService.pageQueryWalletChangeHistory(currentUserId, request, principal);
        return CompletableFuture.completedFuture(result);
    }

}
